/**
 * 判断链表中是否存在环
*/
function detectCycle (head) {
    // 快慢指针
    let fast = head;
    let slow = head;
    while (fast && slow) {
        // 快指针一次性走两步
        fast = fast.next.next;
        // 慢指针一次性走一步
        slow = slow.next;
        // 如果快慢指针相遇则返回true
        if (fast  === slow) {
            return true;
        }
    }
    return false;
}

function Node (data) {
    this.data = data;
    this.next = null;
}

const node3 = new Node(3);
const node2 = new Node(2);
const node0 = new Node(0);
const nodeF4 = new Node(-4);

node3.next = node2;
node2.next = node0;
node0.next = nodeF4;
nodeF4.next = node2;

console.log(detectCycle(node3));
